package com.boatAnchorAssistant.service.bean.exception;

import com.boatAnchorAssistant.pojo.exception.department.DepartmentNameRepeatException;
import com.boatAnchorAssistant.pojo.exception.user.NotAuthException;
import com.boatAnchorAssistant.pojo.exception.user.UserNumberRepeatException;
import com.boatAnchorAssistant.pojo.exception.user.UserPhoneRepeatException;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import nirvana.core.domains.vo.ApiResult;
import nirvana.core.exceptions.GlobalExceptionHand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 【描 述】：
 * 【环 境】：J2SE 17
 * 【详 细】：
 *
 * @author 李松林
 * @version v1.0 2024/5/3 新建
 * @since 2024/5/3
 */
@Component
@Slf4j
public class RegisterExceptionHandler {


    @Autowired
    private GlobalExceptionHand hand;

    @Autowired
    private HttpServletResponse response;


    @PostConstruct
    public void init() {
        // 注册401 异常
        hand.registerExceptionHandler(NotAuthException.class, e -> {
            try {
                response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            return new ApiResult<>(500, e.getMessage());
        });
        // 添加其他拦截方法...


        //部门名称重复
        hand.registerExceptionHandler(DepartmentNameRepeatException.class, e -> {
            log.error("部门名称重复");
            return new ApiResult<>(500, "部门名称重复");
        });

        //用户手机号重复
        hand.registerExceptionHandler(UserPhoneRepeatException.class, e -> {
            log.error("用户手机号重复");
            return new ApiResult<>(500, "用户手机号重复");
        });

        //用户工号重复
        hand.registerExceptionHandler(UserNumberRepeatException.class,e->{
            log.error("");
            return new ApiResult<>(500, "用户工号重复");
        });

    }

}
